//
//  YYHomeVC.m
//  drivingSchool
//
//  Created by 子yu 刘 on 2018/7/29.
//  Copyright © 2018年 子yu 刘. All rights reserved.
//

#import "YYHomeVC.h"
#import "YYHomeCell.h"
#import "YYHomeModel.h"
#import "YYSchoolViewController.h"

@interface YYHomeVC ()<UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate>
@property (nonatomic ,strong)NSArray * dataArr;
@property (strong, nonatomic) SDCycleScrollView *adCycleScrollView;
@end

@implementation YYHomeVC

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    if (@available(iOS 11.0, *)) {//解决隐藏导航栏scrollView不能到顶的问题
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
    [self getData];
    [self getVersion];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
         [self getData];
    }];
}

- (void)setUI{
    SDCycleScrollView *adCycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:self.headerView.bounds delegate:self placeholderImage:[UIImage new]];
    adCycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
    adCycleScrollView.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    adCycleScrollView.currentPageDotColor = [UIColor colorWithHexString:@"08c5ed"];
    adCycleScrollView.pageDotColor = [UIColor colorWithHexString:@"bdbdbd"];
    adCycleScrollView.pageControlDotSize = CGSizeMake(4, 4);  // pageControl小点的大小
    adCycleScrollView.contentMode = UIViewContentModeScaleAspectFill;
    [self.headerView addSubview:adCycleScrollView];
    [adCycleScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(-16);
    }];
    self.adCycleScrollView = adCycleScrollView;
}

-(void)getVersion{
    [RequestOperationManager getVersionSuccess:^(id result) {
        NSString * currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
        if (DEF_REQ_SUCCESS) {//添加成功
            NSString * newVersion = result[@"data"][@"version"];
            if (![currentVersion isEqualToString:newVersion]) {//不是最新版本，弹框
                BOOL toUpdate =  [self compareThisVersion:currentVersion andNewVersion:newVersion];
                if (toUpdate) {
                    if ([result[@"data"][@"force"] boolValue]) {
                        [ASAlertViewController getTipAlertControllerWithTitle:@"发现新版本" message:result[@"data"][@"text"] cancelTitle:@"去更新" viewController:self cancelBlock:^(UIAlertAction *action) {
                            NSString * itunesurl =  [NSString stringWithFormat:@"itms-apps://itunes.apple.com/cn/app/id%@?mt=8",APPID];
                            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:itunesurl]];
                        }];
                    }else{
                        [ASAlertViewController getAlertControllerWithTitle:@"发现新版本" message:result[@"data"][@"text"] sureTitle:@"去更新" cancelTitle:@"暂不更新" viewController:self sureAction:^(UIAlertAction *action) {
                            NSString * itunesurl =  [NSString stringWithFormat:@"itms-apps://itunes.apple.com/cn/app/id%@?mt=8",APPID];
                            
                            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:itunesurl]];
                        } cancelAction:nil];
                    }
                }
            }
        }
        
    } failture:^(NSError *error) {
        
    }];
}

-(BOOL)compareThisVersion:(NSString *)currentVersion andNewVersion:(NSString *)newVersion{
    
    /* NSOrderedAscending = -1  升序  新版本大
     * NSOrderedSame = 0        相等  就是最新版本
     * NSOrderedDescending      降序  老版本大
     */
    NSUInteger  versionIndex = (NSUInteger)[currentVersion compare:newVersion options:NSCaseInsensitiveSearch]; //-1
    if (versionIndex == -1) {//新版本大，去更新
        return YES;
    }else if (versionIndex == 0){//就是最新版本
        return NO;
    }else{//当前版本大于appStore上的最新版本
        return NO;
    }
    
}

- (void)getData{
    
    [RequestOperationManager homeList_Success:^(id result) {
        [self.tableView.mj_header endRefreshing];
        if (DEF_REQ_SUCCESS) {
            self.dataArr = [MTLJSONAdapter modelsOfClass:[YYHomeModel class] fromJSONArray:result[@"data"][@"school_list"] error:nil];
            [self.tableView reloadData];
            
            NSArray  * dataArr = result[@"data"][@"roll"];
            NSMutableArray * imageArr = [NSMutableArray arrayWithCapacity:0];
            for (NSDictionary * image in dataArr) {
                [imageArr addObject:[NSString stringWithFormat:@"%@%@",DEF_INTERFACE_WEBVIEWSERVICE,image[@"image"]]];
            }
            self.adCycleScrollView.imageURLStringsGroup =imageArr;
            
        }
    } failture:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];

    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YYHomeCell * cell = [tableView dequeueReusableCellWithIdentifier:@"YYHomeCell"];
    cell.model = self.dataArr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    YYSchoolViewController * school = (YYSchoolViewController*)[CFUtility getVC:@"YYSchoolViewController" Storyboard:HOME];
    YYHomeModel * model = self.dataArr[indexPath.row];
    school.sid = model.sid;
    [self.navigationController pushViewController:school animated:YES];
}


-(NSArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataArr;
}




@end
